package  
{
	import net.flashpunk.graphics.TiledImage;
	import net.flashpunk.World;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Sfx;
	
	/**
	 * ...
	 * @author Donatello
	 */
	public class GameWorld extends World
	{
		[Embed(source = "snd/fire.mp3")]private const FIRE_MP3:Class;
		private var fireSfx:Sfx;
		
		[Embed(source = "snd/music.mp3")]private const MUSIC_MP3:Class;
		private var musicSfx:Sfx;
		
		
		protected var abed:Abed;
		protected var troy:Troy;
		
		protected var wallTime:Number;
		protected var fireTime:Number;
		
		private var espaciado:Number=60;
		
		private var ladoIzq:Boolean = false;
		private var topIzq:Number;
		
		private var ladoDer:Boolean = false;
		private var topDer:Number;
		
		
		public function GameWorld() 
		{
			wallTime = 3.0;		
			fireTime = 0.0;
			
			abed = new Abed(600,550);
			add(abed);
			
			troy = new Troy(200, 550);
			add(troy);
			
			crearBordes();
			
			topIzq = topDer = 500;
			
			
			fireSfx = new Sfx(FIRE_MP3);
			musicSfx = new Sfx(MUSIC_MP3);
			musicSfx.loop();
			
		}
		
		override public function update():void
		{
			wallTime += FP.elapsed;
			fireTime += FP.elapsed;
			abed.actualizar();
			troy.actualizar();
		
			if(wallTime>3)
			{	
				agregarParedes();
				wallTime = 0.0;
			}
			
			
			if (fireTime >1.5)
			{
				if (FP.rand(50) < 25)
				{
						add(new Fire(FP.rand(336) + 32, 0));
				}
				
				if (FP.rand(50) < 25)
				{
						add(new Fire(FP.rand(336) + 400,0));
				}
				
				if (FP.rand(100) < 25)
				{
						add(new FireBig(FP.rand(336) + 32, 0));
				}
				
				if (FP.rand(100) < 25)
				{
						add(new FireBig(FP.rand(336) + 400,0));
				}
				
				
				
				
				fireTime = 0.0;
			}
			
			
			
			
			
			if ((abed.collide("Fire", abed.x, abed.y)) || (troy.collide("Fire", troy.x, troy.y)) )
			{
				fireSfx.play();
				musicSfx.stop();
				FP.world = new MainMenuWorld();
				removeAll();
				
			}
			
			if ((abed.y>FP.screen.height) ||(troy.y>FP.screen.height) )
			{
				musicSfx.stop();
				FP.world = new MainMenuWorld();
				removeAll();
			}
			
			super.update();
            
		}
		
		public function agregarParedes():void
		{
			if(FP.rand(1000)<250)
			{
					add(new DynamicWall(128, topIzq - espaciado));
					add(new DynamicWall(160, topIzq - espaciado));
					add(new DynamicWall(192, topIzq - espaciado));
			}
			else
			{
				if (ladoIzq)
				{	
					add(new DynamicWall(336, topIzq - espaciado));
					add(new DynamicWall(304, topIzq - espaciado));
					add(new DynamicWall(272, topIzq - espaciado));
				}
				else
				{	
					add(new DynamicWall(32, topIzq - espaciado));
					add(new DynamicWall(64, topIzq - espaciado));
					add(new DynamicWall(96, topIzq - espaciado));
				}
			}
			
			ladoIzq = !ladoIzq;
			
			
			if(FP.rand(1000)<250)
			{
					add(new DynamicWall(528, topIzq - espaciado));
					add(new DynamicWall(560, topIzq - espaciado));
					add(new DynamicWall(592, topIzq - espaciado));
			}
			else
			{
				if (ladoDer)
				{	
					add(new DynamicWall(736, topDer - espaciado));
					add(new DynamicWall(704, topDer - espaciado));
					add(new DynamicWall(672, topDer - espaciado));
				}
				else
				{	
					add(new DynamicWall(432, topDer - espaciado));
					add(new DynamicWall(464, topDer - espaciado));
					add(new DynamicWall(496, topDer - espaciado));
				}
			}
			
			ladoDer= !ladoDer;
		
			topIzq -= espaciado;
			topDer -= espaciado;
			
			
		}
		
			
		public function crearBordes():void
		{
			var i:int=0
			while ( i < FP.screen.width)
			{
				add(new StaticWall(0,i));
				add(new StaticWall(400-32,i));
				add(new StaticWall(400,i));
                add(new StaticWall(768, i));
				add(new DynamicWall(i,608));
				i += 32;	
			}
		}
		
	}

}